﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using help;
using myticket.Properties;
using System.Text.RegularExpressions;

//登录页面
namespace myticket
{
    public partial class loginForm : Form
    {
        List<Point> m_clickpoints=new List<Point>();

        public loginForm()
        {
            InitializeComponent();
        }

        //显示验证码
        void showRandcode()
        {
            Thread t=new Thread(()=>
                {
                    bool continueFlag=true;
                    //Stopwatch sw = new Stopwatch();
                    while (continueFlag)
                    {
                        
                        Stream stream = helper.http.GetImageStream(data.constData.GetRandcodeUrl);
                        if (stream != null)
                        {
                            var codeimage = Image.FromStream(stream);
                            continueFlag=false;
                            this.BeginInvoke(new MethodInvoker(delegate()
                            {
                                randcode.Image = codeimage;//设置图片 
                                
                            })
                          );

                        }
                    }
                    //sw.Start()
                }
            );
            t.IsBackground=true;
            t.Start();
        }

        //检查验证码
        void SendCheckRandCode()
        {
            Thread t = new Thread(() =>
            {
                bool continueFlag = true;
                //Stopwatch sw = new Stopwatch();
                string poststring = "randCode=" + GetRandcodeString() + "&rand=sjrand";
                string result = helper.http.Post(data.constData.checkRandCodeUrl, poststring,
                 data.constData.hostUrl,data.constData.orginUrl, data.constData.refUrl);
                bool flag = result.Contains("status:true");
               if(flag==true)
               {
               }
               else
               {
                   this.BeginInvoke(new MethodInvoker(delegate()
                   {
                       this.warning.Text = "验证码错误";
                   }));
               }
            }
            );
            t.IsBackground = true;
            t.Start();
        }

        //格式化验证码
        string GetRandcodeString()
        {
            StringBuilder randcode = new StringBuilder();
            for (int i = 0; i < m_clickpoints.Count; i++)
            {
                randcode.Append(m_clickpoints[i].X + "," + m_clickpoints[i].Y + ",");
            }
            return randcode.ToString();
        }
        //登陆
        void LoginIn()
        {
            string username = this.name.Text;
            string pwd = this.pwd.Text;
            
            Thread t = new Thread(() =>
            {
                string poststring = "loginUserDTO.user_name=" + username + "&userDTO.password=" + pwd + "&randCode=" + GetRandcodeString();
                string result = helper.http.Post(data.constData.LoginUrl, poststring, data.constData.hostUrl,
                data.constData.orginUrl, data.constData.refUrl);
                bool flag = result.Contains("loginCheck:Y");
                string errmsg = "";
                
                if (!flag)
                {
                    //失败
                    //"message":\["(\\S+)"\]  
                    string pattern = "\"messages\":\\[\"(\\S+)\"\\]";
                    Regex reg = new Regex(pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    Match match = reg.Match(result);
                    if (match.Success)
                    {
                        errmsg = match.Groups[1].Value;
                    }
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        this.warning.Text = errmsg;
                    }));
                }
                else
                {
                    //登陆成功
                    this.DialogResult = DialogResult.OK;
                }
            });
            t.IsBackground = true;
            t.Start();
            

        }


        //登录
        private void btn_login_Click(object sender, EventArgs e)
        {
            if (this.name.Text == "")
            {
                this.warning.Text = "请输入用户名！";
                this.name.Focus();
                return;
            }
            else if (this.pwd.Text =="")
            {
                this.warning.Text = "请输入密码！";
                this.pwd.Focus();
                return;
            }
            else if (m_clickpoints.Count == 0)
            {
                this.warning.Text = "验证码不可为空";
                return;
            }
            this.warning.Text = "正在登陆中……";
            LoginIn();

        }
        //刷新验证码
        private void btn_refresh_Click(object sender, EventArgs e)
        {
            m_clickpoints.Clear();
            showRandcode();
        }

        //加载前
        private void beforeLoad(object sender, EventArgs e)
        {
            m_clickpoints.Clear();
            this.name.Text = "whyzi@qq.com";
            this.pwd.Text= "zyx48973561";
            this.warning.Text = "";
            showRandcode();
        }

        //关闭
        private void LoginClose(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }


        private void checkclick(object sender, MouseEventArgs e)
        {
            var pic = sender as PictureBox;
            //移除
            for (int i = 0; i < m_clickpoints.Count;i++ )
            {
                if (m_clickpoints[i] == pic.Location)
                {
                    m_clickpoints.RemoveAt(i);
                    break;
                }
            }

            pic.Dispose();

        }
        //点击图片
        private void mouseclick(object sender, MouseEventArgs e)
        {
            m_clickpoints.Add(new Point(e.X, e.Y));
            PictureBox pic = new PictureBox();
            pic.Image = Resources.icon_checked;
            pic.Location = new System.Drawing.Point(e.X,e.Y);
            pic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            pic.TabStop = false;
            pic.MouseClick += new System.Windows.Forms.MouseEventHandler(checkclick);
            randcode.Controls.Add(pic);
        }
    }
}
